//
//  GHJokeCollectionViewCell.m
//  VideoPic
//
//  Created by GH on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHJokeCollectionViewCell.h"
#import "NetworkingHandle.h"
#import "GHJokeTableViewCell.h"
#import "GHModelOfJoke.h"
#import "GHSeleJokeVC.h"
#import "HomeViewController.h"
#import "DataBaseHandle.h"

@interface GHJokeCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayData;
@property (nonatomic ,retain)NSString *strId;
@property (nonatomic ,retain)NSString *min_time;
@property (nonatomic ,retain)NSMutableArray *arrMin;


@end

@implementation GHJokeCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {


    self = [super initWithFrame:frame];
    if (self) {
        [self delebase];
        [self data];
        [self createTableView];
        
    }

    return self;
}

- (void)delebase {
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"id.sqlite"];
    
    [[DataBaseHandle shareDataBaseHandle] open:file];
    
    NSArray *array = [[DataBaseHandle shareDataBaseHandle] selectAllWithList:@"number" AndListNumber:2];
    
    if (array.count) {
        self.min_time = array[0][1];
    }else {
        self.min_time = @"6047590978";
    }
    NSString *dropSQL = @"DROP TABLE number";
    [[DataBaseHandle shareDataBaseHandle] updateSQL:dropSQL];



}

- (void)database {

       //创建SQL语句.
    NSString *createTableSQL = @"CREATE TABLE IF NOT EXISTS number(id INTEGER PRIMARY KEY AUTOINCREMENT, content text)";
   
    NSString *SQL = [NSString stringWithFormat:@"insert into number(content) values('%@')", self.min_time];
    
    
    
    [[DataBaseHandle shareDataBaseHandle] updateSQL:createTableSQL];
    [[DataBaseHandle shareDataBaseHandle] updateSQL:SQL];
//
    

}

- (void) data {
    if (self.arrayData == nil) {
        
        self.arrayData = [NSMutableArray array];
    }
    self.arrMin = [NSMutableArray array];
    
    NSString *url = [NSString stringWithFormat:@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-102&iid=3914532857&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App Store&device_platform=iphone&idfa=177896AB-D8CF-4150-99C2-8E562731F6F7&vid=ACE4A108-7577-4471-A722-91852E0C04E1&openudid=80dcf8bba6449077e78a4785ac7799f2aaee1a32&device_type=iPhone 5S&version_code=5.0.1&ac=WIFI&screen_width=640&device_id=12714942149&aid=7&city=辽宁省&content_type=-102&count=30&essence=1&latitude=38.88243659950502&longitude=121.5395926195688&message_cursor=0&min_time=%@&mpic=1",self.min_time];
    
[NetworkingHandle handleWorkData:url AndBody:nil value:nil handleBlock:^(id result) {
    

    NSDictionary *dicData = [result objectForKey:@"data"];
    self.min_time = [dicData objectForKey:@"min_time"];
    NSArray *arrCon = [dicData objectForKey:@"data"];
        for (NSDictionary *dicCon in arrCon) {
       
        if ([[dicCon objectForKey:@"type"] isEqualToNumber:@5]) {
            NSLog(@"广告");
        }else {
        
        
        NSDictionary *dicGroup = [dicCon objectForKey:@"group"];
        GHModelOfJoke *model = [[GHModelOfJoke alloc] init];
        if (dicGroup) {
            
            [self.arrMin addObject:[dicGroup objectForKey:@"id"]];
        }
        model.content = [dicGroup objectForKey:@"content"];
        model.category_name = [dicGroup objectForKey:@"category_name"];
        model.digg_count = [dicGroup objectForKey:@"digg_count"];
        model.bury_count = [dicGroup objectForKey:@"bury_count"];
        model.comm = [dicGroup objectForKey:@"comment_count"];
        model.share_count = [dicGroup objectForKey:@"share_count"];
        NSDictionary *dicUser = [dicGroup objectForKey:@"user"];
        model.name = [dicUser objectForKey:@"name"];
        model.avatar_url = [dicUser objectForKey:@"avatar_url"];
      
        NSArray * arrcount = [dicCon objectForKey:@"comments"];
          //comments
        if (arrcount.count) {
            
            NSDictionary *dicComments = arrcount[0];
//            [model setValuesForKeysWithDictionary:dicComments];

            model.user_name = [dicComments objectForKey:@"user_name"];
            model.user_profile_image_url = [dicComments objectForKey:@"user_profile_image_url"];
            model.text = [dicComments objectForKey:@"text"];
            model.comment_count = [dicComments objectForKey:@"digg_count"];
        }
       
        [self.arrayData addObject:model];
    }
    }
    
    
    
    [self database];
    [self.tableview reloadData];
    
    
    
}];




}


- (void)createTableView {
    self.tableview = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableview];
    
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    
    [self.tableview registerClass:[GHJokeTableViewCell class] forCellReuseIdentifier:@"pool"];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GHJokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GHModelOfJoke *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell passWithModel:model];
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    GHModelOfJoke *model = [self.arrayData objectAtIndex:indexPath.row];
    
    
    return [GHJokeTableViewCell heightForCellWith:model];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.strId = [self.arrMin objectAtIndex:indexPath.row];
    
    GHSeleJokeVC *vc = [[GHSeleJokeVC alloc] init];
    
    id object = [self nextResponder];
    while (![object isKindOfClass:[HomeViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    
    HomeViewController *homeVC = (HomeViewController *)object;

    NSString *url = [NSString stringWithFormat:@"http://isub.snssdk.com/neihan/comments/?iid=3914532857&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App Store&device_platform=iphone&idfa=177896AB-D8CF-4150-99C2-8E562731F6F7&vid=ACE4A108-7577-4471-A722-91852E0C04E1&openudid=80dcf8bba6449077e78a4785ac7799f2aaee1a32&device_type=iPhone 5S&version_code=5.0.1&ac=WIFI&screen_width=640&device_id=12714942149&aid=7&count=20&device_id=12714942149&group_id=%@&offset=0&sort=hot&tag=joke",self.strId];
    
  GHModelOfJoke *model = [self.arrayData objectAtIndex:indexPath.row];
    vc.url = url;
    vc.model = model;
    [homeVC.navigationController pushViewController:vc animated:YES];
    
    
    
//http://isub.snssdk.com/neihan/comments/?iid=3914532857&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=177896AB-D8CF-4150-99C2-8E562731F6F7&vid=ACE4A108-7577-4471-A722-91852E0C04E1&openudid=80dcf8bba6449077e78a4785ac7799f2aaee1a32&device_type=iPhone%205S&version_code=5.0.1&ac=WIFI&screen_width=640&device_id=12714942149&aid=7&count=20&device_id=12714942149&group_id=6047590978&offset=0&sort=hot&tag=joke

}


@end
